using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WuLibrary;


namespace TowerDefence
{
    public class Enemy : MonoBehaviour
    {
        public List<Vector3> mMapPathList = new List<Vector3>();

        public float mMoveSpeed = 1;

        public int mHP = 10;
        
        public int mMaxHP = 10;

        public EnemyHpItem EnemyHpItem;
        
        public Transform mHpPos;


        public void InitData(List<Vector3> path,int hp,float moveSpeed)
        {
            mMapPathList.AddRange(path);
            if (mMapPathList.Count > 0)
            {
                transform.localPosition = mMapPathList[0];
                mMapPathList.RemoveAt(0);
            }

            mMaxHP = mHP = hp;
            mMoveSpeed=moveSpeed;
            
            Animation anim=GetComponent<Animation>();
            anim.PlayQueued("RunFront", QueueMode.PlayNow);
            mHpPos = transform.Find("hp_pos");
        }

        public void BattleUpdate()
        {
            if (mMapPathList.Count > 0)
            {
                Vector3 pos=mMapPathList[0];

                float rang;
                //Z轴行走
                if (transform.localPosition.x == pos.x)
                {
                     rang=Mathf.Abs(transform.localPosition.z-pos.z);
                     if (transform.localPosition.z > pos.z)//下
                     {
                         transform.localEulerAngles = new Vector3(0, 180, 0);
                     }
                     else//上
                     {
                         transform.localEulerAngles = new Vector3(0, 0, 0);
                     }
                    
                }
                else//X轴行走
                {
                    pos.z=transform.localPosition.z; 
                    rang=Mathf.Abs(transform.localPosition.x-pos.x);
                    if (transform.localPosition.x > pos.x)//左边
                    {
                        transform.localEulerAngles = new Vector3(0, 270, 0);
                    }
                    else//右边
                    {
                        transform.localEulerAngles = new Vector3(0, 90, 0);
                    }
                    
                }
                float totalTime = rang / mMoveSpeed;
                if (Time.deltaTime >= totalTime)
                {
                    transform.localPosition = pos;
                    if (transform.localPosition == mMapPathList[0])
                    {
                        mMapPathList.RemoveAt(0);
                    }
                }
                else
                    transform.localPosition = Vector3.Lerp(transform.localPosition,pos, Time.deltaTime / totalTime);
                
            }
            else
            {
                mHP = 0;
                DeleteObj();
                BattleManager.EnemyAttack();
                    
            }
        }

        public void AddHP(int hp)
        {
            mHP += hp; 
            
            if (mHP <= 0)
            {
                DeleteObj();
                
            }
            EnemyHpItem.RefeshHp();
        }

        public void DeleteObj()
        {
            Destroy(gameObject);
        }
    }
}

